#include <conio.h>
#include "../../HeadFiles/StandardHeads.h"
#include "../../HeadFiles/PagesH/CommodityPage.h"
#include "../../HeadFiles/ChainsH/CommodityChain.h"
#include "../../HeadFiles/StrSimilar.h"
#include "../../HeadFiles/InputCheck.h"

extern struct CommodityNode * commodityHead;
extern struct CommodityNode * commodityTail;
extern struct CommodityNode * commodityDisplayPtr;

extern struct RecordNode * recordHead; // 用来查相关记录

// return 1是保留，0是返回上一级
int CommodityPage()
{
    system("cls");
    puts("Now displaying commodity list:");
    printf("type 1 for the previous\n");
    printf("type 2 for the next\n");
    printf("type 3 to delete this one\n");
    printf("type 4 to add one\n");
    printf("type 5 to display all related records\n");
    printf("type 0 to get back\n");
    // checkHeadAndTail();
    puts(SPLIT_STR);

    if(commodityDisplayPtr != NULL)
        displayThisCommodity(commodityDisplayPtr);   // 先展示最新的一条
    else{
        puts("No commodity in database! Add one first or get back!");
        goto Add_Commodity_Page;
        // return 2;
    }

    char opt2 = getche();
    while(!(opt2 == '1' || opt2 == '2' || opt2 == '3' || opt2 == '4' || opt2 == '5' || opt2 == '0'))
    {
        puts("Illegal input detected! Try again:");
        opt2 = getche();
    }

    // opt2已经合法
    if(opt2 == '2')
    {
        // 查询下一条记录
        if(commodityDisplayPtr->nextCommodity!= NULL)
        {
            commodityDisplayPtr = commodityDisplayPtr->nextCommodity;
            displayThisCommodity(commodityDisplayPtr);
            // goto Client_Page;
            return 1;
        }else{
            puts("No next commodity! Getting back to the previous page!");
            system("pause");
            // goto Client_Page;
            return 1;
        }
    }else if(opt2 == '1'){
        // 查询上一条记录
        if(commodityDisplayPtr->preCommodity != NULL)
        {
            commodityDisplayPtr = commodityDisplayPtr->preCommodity;
            displayThisCommodity(commodityDisplayPtr);
            // goto Client_Page;
            return 1;
        }else{
            puts("No previous commodity! Getting back to the previous page!");
            system("pause");
            // goto Record_Page;
            return 1;
        }
    }else if(opt2 == '3'){
        // 删除这一条
        //authorize!
        struct CommodityNode * tempPtr = NULL;
        if(commodityDisplayPtr->preCommodity == NULL && commodityDisplayPtr->nextCommodity == NULL)
        {
            tempPtr = NULL;   // 最后一个节点被删除了
        }else{
            // 如果不是最后一个，则去往任意一个不为空的节点即可
            if(commodityDisplayPtr->nextCommodity != NULL)
                tempPtr = commodityDisplayPtr->nextCommodity;
            else
                tempPtr = commodityDisplayPtr->preCommodity;
        }
        delCommodity(&commodityHead, &commodityTail, commodityDisplayPtr);
        commodityDisplayPtr = tempPtr;
        // goto Client_Page;
        rebuildCommodityFile(commodityHead);
        return 1;
    }else if(opt2 == '4'){
        // 新增一条
        Add_Commodity_Page:
        addCommodity(&commodityHead, &commodityTail);
        getUnitCount(&recordHead , commodityTail);
        rebuildCommodityFile(commodityHead);
        commodityDisplayPtr = commodityTail;
        // goto Client_Page;
        return 1;
    }else if(opt2 == '5'){
        // 查询相关记录
        struct RecordNode * searchPtr = recordHead;
        while(searchPtr != NULL)
        {
            char s1[STR_MAX_LEN];
            memset(s1, 0, sizeof s1);
            memcpy(s1, searchPtr->commodityName, sizeof(searchPtr->commodityName));

            char s2[STR_MAX_LEN];
            memset(s2, 0, sizeof s2);
            memcpy(s2, commodityDisplayPtr->commodityName, sizeof(commodityDisplayPtr->commodityName));

            double f = getStrSimilarity(s1, s2);
            // printf("%s\n%s\nf=%.2f\n", s1, s2, f);
            // 此处可能还要取相似度最大值且大于等于0.9
            if(f >= 0.9)
                displayThisRecord(searchPtr);
            searchPtr = searchPtr->nextRecord;
        }
        puts("Display Finished");
        system("pause");
        return 1;
    }else{
        // 返回上一页
        // goto Welcome_Page;
        return 0;
    }
}